package com.lingnan.eduservice.controller;


import com.lingnan.common.utils.ResultEntity;
import com.lingnan.eduservice.entity.po.EduVideoPO;
import com.lingnan.eduservice.service.EduVideoService;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 小节管理
 *
 * @author zzd
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/eduservice/eduvideo")
@Api(value = "小节管理")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 添加小节
     *
     * @param eduVideoPO
     * @return
     */
    @ApiOperation("添加小节")
    @PostMapping("addVideo")
    public ResultEntity addVideo(@RequestBody EduVideoPO eduVideoPO) {
        // 执行添加功能
        try {
            eduVideoService.save(eduVideoPO);

            // 添加成功
            return ResultEntity.ok();
        } catch (Exception e) {

            e.printStackTrace();
            // 添加失败
            return ResultEntity.error();
        }

    }

    /**
     * 根据小节ID删除小节功能(包括阿里中上传的视频)
     *
     * @param videoId
     * @return
     */
    @ApiOperation("根据小节ID删除小节功能")
    @DeleteMapping("deleteVideoById/{videoId}")
    public ResultEntity deleteVideoById(@PathVariable String videoId) {
        try {
            // 执行删除操作
            eduVideoService.deleteVideoById(videoId);
            // 返回成功信息
            return ResultEntity.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 返回失败信息
            return ResultEntity.error();
        }
    }

    /**
     * 根据小节ID修改小节
     *
     * @param eduVideoPO
     * @return
     */
    @ApiOperation("根据小节ID修改小节")
    @PostMapping("updateVideoById")
    public ResultEntity updateVideo(@RequestBody EduVideoPO eduVideoPO) {
        try {
            // 执行更新操作
            eduVideoService.updateById(eduVideoPO);

            // 返回更新成功信息
            return ResultEntity.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 返回更新失败信息
            return ResultEntity.error();
        }
    }

    /**
     * 根据小节id查询小节信息
     * @param videoId
     * @return
     */
    @ApiOperation("根据小节id查询小节信息")
    @GetMapping("findVideoById/{videoId}")
    public ResultEntity findVideoById(@PathVariable String videoId) {
        // 执行查询
        EduVideoPO eduVideoPO = eduVideoService.getById(videoId);
        if (null == eduVideoPO) {
            throw new LingNanException(20001, "没有查询到该信息");
        }
        // 返回查询信息
        return ResultEntity.ok().data("eduVideoPO", eduVideoPO);
    }



}

